<?php
/**
 * Created by 陈艺赫.
 * User: Vvvvv
 * Date: 2017/12/9
 * Time: 11:30
 */

namespace Admin\Controller;


class DownloadController extends BaseController
{
    public function index()
    {
        $db_file_cat = M('file_cat');
        $data_file_cat = $db_file_cat->where('status = 1')->select();
        $tab = !empty($_GET['tab']) ? $_GET['tab'] : '1';

        $this->assign('tab', $tab);//选项卡
        $this->assign('cat', $data_file_cat);
        $this->display();
    }

    public function dataGet()
    {
        if (IS_GET) {
            $db_file_cat = M('file_cat');
            $limit = I('get.limit');
            $page = I('get.page');
            $count = $db_file_cat->where('status = 1')->count();
            $data_file_cat = $db_file_cat->where('status = 1')->page($page, $limit)->select();
            $data['data'] = $data_file_cat;
            $data['code'] = 0;
            $data['count'] = $count;
            $this->ajaxReturn($data);
        }
        $this->error('error');

    }

    public function fileDataGet()
    {
        if (IS_GET) {
            $db_file_cat = D('File');
            $limit = I('get.limit');
            $page = I('get.page');
            $count = $db_file_cat->where('status = 1')->count();
            $data_file_cat = $db_file_cat->relation(true)->where('status = 1')->page($page, $limit)->select();
            foreach ($data_file_cat as $k=>$v){
                $data_file_cat[$k]['cat_name'] = $v['cat']['name'];
            }
            $data['data'] = $data_file_cat;
            $data['code'] = 0;
            $data['count'] = $count;
            $this->ajaxReturn($data);
        }
        $this->error('error');

    }

    public function delCat()
    {
        if (IS_POST) {
            $id = I('post.id');
            $db_file_cat = M('file_cat');
            $data_file_cat = $db_file_cat->where('id = ' . $id)->find();
            $data_file_cat['status'] = 0;
            $result = $db_file_cat->save($data_file_cat);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function editCat()
    {
        if (IS_POST) {
            $id = I('post.id');
            $field = I('post.field');
            $value = I('post.value');
            $db_file_cat = M('file_cat');
            $data_file_cat = $db_file_cat->where('id = ' . $id)->find();
            $data_file_cat[$field] = $value;
            $result = $db_file_cat->save($data_file_cat);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function catAdd()
    {
        if (IS_POST) {
            $data['name'] = I('post.name');
            $data['order'] = I('post.order');
            $db_name = M('file_cat');
            $result = $db_name->add($data);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function fileUpload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2097152;// 设置附件上传大小
        $upload->exts = array('doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx');// 设置附件上传类型
        $upload->rootPath = './Public/upload/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->saveName = time() . '';
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            $this->success('/Public/upload/' . $info['savename']);
        }
    }

    public function uploadSubmit()
    {
        if (IS_POST) {
            $data['name'] = I('post.filename');
            $data['order'] = I('post.fileorder');
            $data['path'] = I('post.path');
            $data['cat_id'] = I('post.cat_id');
//            $data['cat_id'] = I('cat_id');
            $db_name = M('file');
            $result = $db_name->add($data);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function delFile()
    {
        if (IS_POST) {
            $id = I('post.id');
            $db_file_cat = M('file');
            $data_file_cat = $db_file_cat->where('id = ' . $id)->find();
            $data_file_cat['status'] = 0;
            $result = $db_file_cat->save($data_file_cat);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function editFile(){
        if (IS_GET) {
            $id = I('get.id');
            $db_span = M('file');
            $db_file_cat = M('file_cat');
            $data_span = $db_span->where('id = ' . $id)->find();
            $data_file_cat = $db_file_cat->where('status = 1')->select();
            $this->assign('cat', $data_file_cat);
            $this->assign('data',$data_span);
            $this->display();
        }
    }

    public function editFilePost()
    {
        if (IS_POST) {
            $db_file = M('file');
            $data = I('post.');
            $result = $db_file->save($data);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }
}